package com.manong.bookstore.message.service.impl;

import com.alibaba.nacos.client.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manong.common.utils.PageUtils;
import com.manong.common.utils.Query;

import com.manong.bookstore.message.dao.MessageDao;
import com.manong.bookstore.message.entity.MessageEntity;
import com.manong.bookstore.message.service.MessageService;


@Service("messageService")
public class MessageServiceImpl extends ServiceImpl<MessageDao, MessageEntity> implements MessageService {

//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<MessageEntity> page = this.page(
//                new Query<MessageEntity>().getPage(params),
//                new QueryWrapper<MessageEntity>()
//        );
//
//        return new PageUtils(page);
//    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //1、获取key
        String key = (String) params.get("key");
        QueryWrapper<MessageEntity> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(key)) {
            queryWrapper.eq("message_id", key).or().eq("send_id", key).or().eq("receive_id", key).or().like("send_type", key).or().like("receive_type", key).or().like("content", key);
        }

        IPage<MessageEntity> page = this.page(
                new Query<MessageEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils querylikePage(Map<String, Object> params) {
        IPage<MessageEntity> page = this.page(
                new Query<MessageEntity>().getPage(params),
                new QueryWrapper<MessageEntity>()
                        .like("message_id", params.get("messageIdValue"))
                        .like("send_id", params.get("sendIdValue"))
                        .like("receive_id", params.get("receiveIdValue"))
                        .like("send_type", params.get("sendTypeValue"))
                        .like("receive_type", params.get("receiveTypeValue"))
                        .like("content", params.get("contentValue"))
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryExactPage(Map<String, Object> params) {
        for (Map.Entry<String, Object> entry : params.entrySet()
                ) {
            if (entry.getValue() == "") {
                entry.setValue("is not null");
            }
        }
        IPage<MessageEntity> page = this.page(
                new Query<MessageEntity>().getPage(params),
                new QueryWrapper<MessageEntity>()
                        .eq("message_id", params.get("messageIdValue"))
                        .eq("send_id", params.get("sendIdValue"))
                        .eq("receive_id", params.get("receiveIdValue"))
                        .eq("send_type", params.get("sendTypeValue"))
                        .eq("receive_type", params.get("receiveTypeValue"))
                        .eq("content", params.get("contentValue"))

        );

        return new PageUtils(page);
    }


}